package com.xhwl.logistics.service.business;

import com.xhwl.common.enums.device.DeviceOnlineStatusEnum;
import com.xhwl.common.pojo.cent.device.Device;
import com.xhwl.logistics.bo.cent.DeviceBO;
import com.xhwl.logistics.converter.mapstruct.CentDeviceConverter;
import com.xhwl.logistics.dto.http.DeviceInfoCoSonDTO;
import com.xhwl.logistics.dto.push.DeviceStatusKeTopDTO;
import com.xhwl.logistics.service.cent.QueryDeviceSecondService;
import com.xhwl.logistics.service.impl.kafka.VehicleAlarmKafkaServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;

//@Service
public class VehicleDeviceDataPushBusinessService {

    @Autowired
    private VehicleAlarmKafkaServiceImpl vehicleAlarmKafkaService;

    @Autowired
    private QueryDeviceSecondService queryDeviceSecondService;

    @Autowired
    private CentDeviceConverter centDeviceConverter;

    /**
     * 用于更新设备状态，并发起离线告警，通过第三方设备id
     */
    public void usualDeviceStatusByThirdDeviceId(String thirdDeviceId, String status, Integer projectId){
        DeviceBO deviceBO = queryDeviceSecondService.queryDeviceByProjectIdAndThirdDeviceId(projectId, thirdDeviceId);

        Device device = centDeviceConverter.deviceBO2Device(deviceBO);

        if(device == null){
            return;
        }
        if ("0".equals(status)) {
            device.setOnlineStatus(DeviceOnlineStatusEnum.OFFLINE);

            vehicleAlarmKafkaService.sendAlarmForVehicleDeviceStatusOffline(projectId, device.getId());
        } else if ("1".equals(status)) {
            device.setOnlineStatus(DeviceOnlineStatusEnum.ONLINE);
        } else {
            throw new IllegalStateException("设备状态上报的状态值，不是文档规定的0，1");
        }

        queryDeviceSecondService.updateDeviceStatus(device);
    }

    /**
     * 用于更新设备状态，并发起离线告警，通过设备ip
     */
    public void usualDeviceStatus(String deviceIp, String status, Integer projectId){
        DeviceBO deviceBO = queryDeviceSecondService.queryDevicesByProjectIdAndDeviceNumber(projectId, deviceIp);

        Device device = centDeviceConverter.deviceBO2Device(deviceBO);

        if(device == null){
            return;
        }

        if ("0".equals(status)) {
            device.setOnlineStatus(DeviceOnlineStatusEnum.OFFLINE);

            vehicleAlarmKafkaService.sendAlarmForVehicleDeviceStatusOffline(projectId, device.getId());
        } else if ("1".equals(status)) {
            device.setOnlineStatus(DeviceOnlineStatusEnum.ONLINE);
        } else {
            throw new IllegalStateException("设备状态上报的状态值，不是文档规定的0，1");
        }

        queryDeviceSecondService.updateDeviceStatus(device);
    }

    public void keTopDeviceStatus(DeviceStatusKeTopDTO deviceStatusKeTopDTO, Integer projectId) {
        DeviceBO deviceBO = queryDeviceSecondService.queryDevicesByProjectIdAndDeviceNumber(projectId, deviceStatusKeTopDTO.getDeviceIp());

        Device device = centDeviceConverter.deviceBO2Device(deviceBO);

        String status = deviceStatusKeTopDTO.getStatus();
        if ("0".equals(status)) {
            device.setOnlineStatus(DeviceOnlineStatusEnum.OFFLINE);

            vehicleAlarmKafkaService.sendAlarmForVehicleDeviceStatusOffline(projectId, device.getId());
        } else if ("1".equals(status)) {
            device.setOnlineStatus(DeviceOnlineStatusEnum.ONLINE);
        } else {
            throw new IllegalStateException("设备状态上报的状态值，不是文档规定的0，1");
        }

        queryDeviceSecondService.updateDeviceStatus(device);
    }

    public void coSonDeviceStatus(DeviceInfoCoSonDTO deviceInfoCoSonDTO,Integer projectId){
        DeviceBO deviceBO = queryDeviceSecondService.queryDevicesByProjectIdAndDeviceNumber(projectId, deviceInfoCoSonDTO.getVehNumber());

        Device device = centDeviceConverter.deviceBO2Device(deviceBO);

        String status = deviceInfoCoSonDTO.getVehStatus();
        if ("0".equals(status)) {
            device.setOnlineStatus(DeviceOnlineStatusEnum.OFFLINE);

            vehicleAlarmKafkaService.sendAlarmForVehicleDeviceStatusOffline(projectId, device.getId());
        } else if ("1".equals(status)) {
            device.setOnlineStatus(DeviceOnlineStatusEnum.ONLINE);
        } else {
            throw new IllegalStateException("设备状态上报的状态值，不是文档规定的0，1");
        }

        queryDeviceSecondService.updateDeviceStatus(device);
    }
}
